
###Clear R--------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation---------------------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("ggplot2", "psych","interplot","stargazer","dplyr","stringr","tidyr", "xtable")
ipak(packages)

###Load data-----------
load("Study2_data.RData")

###Descriptive statistics in the paper----------------
table(data$pid)
647+655
prop.table(table(data$pid))

table(data$Q31)
table(data$Q34)
633+13
634+21

646/(646+655)

## other demos
prop.table(table(data$education))
prop.table(table(data$female))
prop.table(table(data$non_white))

#Partisan Social Identity Strength
summary(data$pidstrength)
sd(data$pidstrength, na.rm=T)
skewness(data$pidstrength, na.rm=T)
kurtosis(data$pidstrength, na.rm=T)

dp <-with(data,data.frame(pid1,pid2,pid3,pid3.1,Q19,pid4,pid5,pid6))
dp[dp==""]     = NA
dp <- colwise(droplevels)(dp)
dp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(dp)
colnames(data)

dp <- colwise(.fun = function(x)as.numeric(factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree"))))(dp)
dp <- colwise(as.numeric)( dp)

rp <-with(data,data.frame(Q20,Q21,Q22,Q23,Q24,Q25,Q26.1,Q27.1))
rp[rp==""]     = NA
rp <- colwise(function(x)factor(x,levels=c("Strongly disagree","Somewhat disagree","Neither agree nor disagree","Somewhat agree","Strongly agree")))(rp)
rp <- colwise(as.numeric)( rp)
data$reppid <- rowMeans(rp,na.rm=T)
data$huddy1 <- ifelse(!is.na(dp$pid1),dp$pid1,rp$Q20)
data$huddy2 <- ifelse(!is.na(dp$pid2),dp$pid2,rp$Q21)
data$huddy3 <- ifelse(!is.na(dp$pid3),dp$pid3,rp$Q22)
data$huddy4 <- ifelse(!is.na(dp$pid3.1),dp$pid3.1,rp$Q23)
data$huddy5 <- ifelse(!is.na(dp$Q19),dp$Q19,rp$Q24)
data$huddy6 <- ifelse(!is.na(dp$pid4),dp$pid4,rp$Q25)
data$huddy7 <- ifelse(!is.na(dp$pid5),dp$pid5,rp$Q26.1)
data$huddy8 <- ifelse(!is.na(dp$pid6),dp$pid6,rp$Q27.1)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),check.keys = T)


#Cognitive Reflection Test
mean(data$CRTall)
sd(data$CRTall)
skewness(data$CRTall)
kurtosis(data$CRTall)
psych::alpha(with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7)))

#Need for Cognition
mean(data$nfc)
sd(data$nfc)
skewness(data$nfc)
kurtosis(data$nfc)
nfcvars <-(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18))))
nfcvars[,c(3,4,5,7,8,9,12,16,17)] <-   1-nfcvars[,c(3,4,5,7,8,9,12,16,17)]
psych::alpha(nfcvars)

#Political knowledge
mean(data$pk)
sd(data$pk)
skewness(data$pk)
kurtosis(data$pk)
data$pk1_num <-as.numeric(data$pk1)
data$pk2_num <-as.numeric(data$pk2)
data$pk3_num <-as.numeric(data$pk3)
data$pk4_num <-as.numeric(data$pk4)
data$pk5_num <-as.numeric(data$pk5)
data$pk6_num <-as.numeric(data$pk6)
data$pk7_num <-as.numeric(data$pk7)
data$pk8_num <-as.numeric(data$pk8)
data$pk9_num <-as.numeric(data$pk9)
data$pk10_num <-as.numeric(data$pk10)
psych::alpha(with(data,data.frame(pk1_num,pk2_num,pk3_num,pk4_num,pk5_num,pk6_num,pk7_num,pk8_num,pk9_num,pk10_num)))

#Latent cognitive resources
mean(data$cogresources)
sd(data$cogresources)
skewness(data$cogresources)
kurtosis(data$cogresources)
psych::alpha(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7,nfc_1,nfc_2,nfc_3,nfc_4,nfc_5,nfc_6,nfc_7,nfc_8,nfc_9,nfc_10,nfc_11,nfc_12,nfc_13,nfc_14,nfc_15,nfc_16,nfc_17,nfc_18, pk1, pk2, pk3, pk4, pk5, pk6, pk7, pk8, pk9, pk10))),check.keys = T) 

# Correlations reported in main text 
cor.test(data$CRTall, data$nfc)
cor.test(data$CRTall, data$pk)
cor.test(data$CRTall, data$cogresources)

cor.test(data$pk, data$nfc)
cor.test(data$pk, data$cogresources)
cor.test(data$nfc, data$cogresources)

cor.test(data$pidstrength, data$CRTall)
cor.test(data$pidstrength, data$nfc)
cor.test(data$pidstrength, data$pk)
cor.test(data$pidstrength, data$cogresources)

#Distrubtion dv
summary(data$DV1)
sd(data$DV1)


### Main effects of treatment on policy support-----------------------------
summary(lm(DV1~InParty+OutParty+OutParty+pidstrength+zero1(CRTall)+age+female+ non_white +as.factor(education)+Republican_dummy,data))
summary(lm(DV1~InParty+OutParty+OutParty+pidstrength+nfc+age+female+ non_white +as.factor(education)+Republican_dummy,data))
summary(lm(DV1~InParty+OutParty+OutParty+pidstrength+pk+age+female+ non_white +as.factor(education)+Republican_dummy,data))
summary(lm(DV1~InParty+OutParty+OutParty+pidstrength+cogresources+age+female+ non_white +as.factor(education)+Republican_dummy,data))

### Table 3: Farm Subsidy Experiment: Policy support, party cues, reflection and social identity strength-----------------------------

twoway_crt<-(lm(DV1~InParty+OutParty+pidstrength+zero1(CRTall)+InParty*pidstrength+OutParty*pidstrength+InParty*zero1(CRTall)+OutParty*zero1(CRTall)+pidstrength*zero1(CRTall)+age+female+ non_white +as.factor(education)+Republican_dummy,data))
twoway_nfc<-(lm(DV1~InParty+OutParty+pidstrength+nfc+InParty*pidstrength+OutParty*pidstrength+InParty*nfc+OutParty*nfc+pidstrength*nfc+age+female+ non_white +as.factor(education)+Republican_dummy,data))
twoway_pk<-(lm(DV1~InParty+OutParty+pidstrength+pk+InParty*pidstrength+OutParty*pidstrength+InParty*pk+OutParty*pk+pidstrength*pk+age+female+ non_white +as.factor(education)+Republican_dummy,data))
twoway_cog<-(lm(DV1~InParty+OutParty+pidstrength+cogresources+InParty*pidstrength+OutParty*pidstrength+InParty*cogresources+OutParty*cogresources+pidstrength*cogresources+age+female+ non_white +as.factor(education)+Republican_dummy,data))

#replace names 2 way model
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "nfc"] <- "zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "InParty:nfc"] <- "InParty:zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "OutParty:nfc"] <- "OutParty:zero1(CRTall)"
names(twoway_nfc$coefficients)[names(twoway_nfc$coefficients) == "pidstrength:nfc"] <- "pidstrength:zero1(CRTall)"

names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "pk"] <- "zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "InParty:pk"] <- "InParty:zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "OutParty:pk"] <- "OutParty:zero1(CRTall)"
names(twoway_pk$coefficients)[names(twoway_pk$coefficients) == "pidstrength:pk"] <- "pidstrength:zero1(CRTall)"

names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "cogresources"] <- "zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "InParty:cogresources"] <- "InParty:zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "OutParty:cogresources"] <- "OutParty:zero1(CRTall)"
names(twoway_cog$coefficients)[names(twoway_cog$coefficients) == "pidstrength:cogresources"] <- "pidstrength:zero1(CRTall)"

#3way interactions
crt_full<-(lm(DV1~InParty+OutParty+pidstrength+zero1(CRTall)+InParty*pidstrength+OutParty*pidstrength+InParty*zero1(CRTall)+OutParty*zero1(CRTall)+pidstrength*zero1(CRTall)+InParty*pidstrength*zero1(CRTall) + OutParty*pidstrength*zero1(CRTall)+age+female+ non_white +as.factor(education)+Republican_dummy,data))
nfc_full<-(lm(DV1~InParty+OutParty+pidstrength+nfc+InParty*pidstrength+OutParty*pidstrength+InParty*nfc+OutParty*nfc+pidstrength*nfc+InParty*pidstrength*nfc + OutParty*pidstrength*nfc+age+female+ non_white +as.factor(education)+Republican_dummy,data))
pk_full<-(lm(DV1~InParty+OutParty+pidstrength+pk+InParty*pidstrength+OutParty*pidstrength+InParty*pk+OutParty*pk+pidstrength*pk+InParty*pidstrength*pk + OutParty*pidstrength*pk+age+female+ non_white +as.factor(education)+Republican_dummy,data))
cog_full<-(lm(DV1~InParty+OutParty+pidstrength+cogresources+InParty*pidstrength+OutParty*pidstrength+InParty*cogresources+OutParty*cogresources+pidstrength*cogresources+InParty*pidstrength*cogresources + OutParty*pidstrength*cogresources+age+female+ non_white +as.factor(education)+Republican_dummy,data))

names(nfc_full$coefficients)[names(nfc_full$coefficients) == "nfc"] <- "zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "InParty:nfc"] <- "InParty:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "OutParty:nfc"] <- "OutParty:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "pidstrength:nfc"] <- "pidstrength:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "InParty:pidstrength:nfc"] <- "InParty:pidstrength:zero1(CRTall)"
names(nfc_full$coefficients)[names(nfc_full$coefficients) == "OutParty:pidstrength:nfc"] <- "OutParty:pidstrength:zero1(CRTall)"

names(pk_full$coefficients)[names(pk_full$coefficients) == "pk"] <- "zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "InParty:pk"] <- "InParty:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "OutParty:pk"] <- "OutParty:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "pidstrength:pk"] <- "pidstrength:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "InParty:pidstrength:pk"] <- "InParty:pidstrength:zero1(CRTall)"
names(pk_full$coefficients)[names(pk_full$coefficients) == "OutParty:pidstrength:pk"] <- "OutParty:pidstrength:zero1(CRTall)"

names(cog_full$coefficients)[names(cog_full$coefficients) == "cogresources"] <- "zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "InParty:cogresources"] <- "InParty:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "OutParty:cogresources"] <- "OutParty:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "pidstrength:cogresources"] <- "pidstrength:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "InParty:pidstrength:cogresources"] <- "InParty:pidstrength:zero1(CRTall)"
names(cog_full$coefficients)[names(cog_full$coefficients) == "OutParty:pidstrength:cogresources"] <- "OutParty:pidstrength:zero1(CRTall)"


stargazer(twoway_crt, crt_full,twoway_nfc, nfc_full, twoway_pk, pk_full, twoway_crt, cog_full, title="Farm Subsidy Experiment: Policy support, party cues, reflection and social identity strength", align=TRUE, order=c(1,2,3,4, 11,12,13,14,15,16,17,5,6,7,8,9,10), covariate.labels=c("In-party cue", "Out-party cue", "Partisan Identity Strength (PSID)","Cognitive resource", "In-party * PSID", "Out-party * PSID", "In-party * Cognitive", "Out-party * Cognitive", "PSID * Cognitive", "In-party * PSID * Cognitive", "Out-party * PSID * Cognitive", "Age", "Female", "Race: non-white", "Education: Some college", "Education: College", "Party: Republican", "Constant"), omit.stat=c("LL","ser","f", "adj.rsq"), 
          notes.append = FALSE, 
          star.cutoffs=c(0.1, 0.05), star.char = c("+", "*"),
          notes = "+p<.1; *p<0.05", column.sep.width = "1pt",
          no.space=TRUE,font.size="tiny" , out = "Study3_summary.tex",dep.var.caption = "Policy support",column.labels = c("CRT", "NFC", "Knowledge", "Cog resources"), column.separate = c(2, 2, 2,2), dep.var.labels.include = F,label="tab:stud3", digits=2)

### Figure 2: Farm Subsidy Experiment: Marginal Effect of In-party and Out-party Cues on Policy Support across Levels of Partisan Social Identity and Cognitive Resources-------------------------------------

## CRT at 0 or -1SD
m0 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T)-sd(zero1(CRTall),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T)-sd(zero1(CRTall),na.rm=T),scale=F)*pidstrength +age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m0)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'
summary(m0)
## CRT at mean
m1 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength+OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall),na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m1)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## CRT at +1
m2 <- lm(DV1~InParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength+ OutParty*scale(zero1(CRTall),center=mean(zero1(CRTall))+sd(zero1(CRTall)),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m2)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot$battery      <- c("CRT")
forplot$CRT <- factor(forplot$CRT,levels = c("-1 SD","Mean","+1 SD"))

#NFC
## NFC -1SD
m0_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*pidstrength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)-sd(nfc,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='pidstrength')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='pidstrength')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

## NFC at mean
m1_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*pidstrength+OutParty*scale(nfc,center=mean(nfc,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='pidstrength')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='pidstrength')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## NFC at +1
m2_nfc <- lm(DV1~InParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*pidstrength+ OutParty*scale(nfc,center=mean(nfc,na.rm=T)+sd(nfc,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m2_nfc)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='pidstrength')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='pidstrength')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_nfc <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_nfc$battery      <- c("NfC")

##PK
## PK at -1SD
m0 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T)-sd(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T)-sd(pk,na.rm=T),scale=F)*pidstrength +age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_out <- interplot(m0,var1 = "OutParty",var2='pidstrength')
m_0_out$data$CRT='-1 SD'
m_0_out$data$Cue='Out'
m_0_in <- interplot(m0,var1 = "InParty",var2='pidstrength')
m_0_in$data$CRT='-1 SD'
m_0_in$data$Cue='In'
## PK at mean
m1 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_out <- interplot(m1,var1 = "OutParty",var2='pidstrength')
m_1_out$data$CRT='Mean'
m_1_out$data$Cue='Out'
m_1_in <- interplot(m1,var1 = "InParty",var2='pidstrength')
m_1_in$data$CRT='Mean'
m_1_in$data$Cue='In'

## PK at +1
m2 <- lm(DV1~InParty*scale(pk,center=mean(pk,na.rm=T)+sd(pk,na.rm=T),scale=F)*pidstrength+OutParty*scale(pk,center=mean(pk,na.rm=T)+sd(pk,na.rm=T),scale=F)*pidstrength +age+female+ non_white +as.factor(education)+Republican_dummy,data)
summary(m2)
m_2_out <- interplot(m2,var1 = "OutParty",var2='pidstrength')
m_2_out$data$CRT='+1 SD'
m_2_out$data$Cue='Out'
m_2_in <- interplot(m2,var1 = "InParty",var2='pidstrength')
m_2_in$data$CRT='+1 SD'
m_2_in$data$Cue='In'

forplot_pk <- rbind(m_0_in$data, m_0_out$data,m_1_in$data,m_1_out$data,m_2_in$data,m_2_out$data)
forplot_pk$battery      <- c("Political\n knowledge")
forplot_pk$CRT <- factor(forplot_pk$CRT,levels = c("-1 SD","Mean","+1 SD"))

##COgnitive resources
## Cog resources -1SD
m0_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T)-sd(cogresources,na.rm=T),scale=F)*pidstrength+ OutParty*scale(cogresources,center=mean(cogresources,na.rm=T)-sd(cogresources,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_0_in_nfc <- interplot(m0_nfc,var1 = "InParty",var2='pidstrength')
m_0_in_nfc$data$CRT='-1 SD'
m_0_in_nfc$data$Cue='In'
m_0_out_nfc <- interplot(m0_nfc,var1 = "OutParty",var2='pidstrength')
m_0_out_nfc$data$CRT='-1 SD'
m_0_out_nfc$data$Cue='Out'

##  Cog resources at mean
m1_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*pidstrength+OutParty*scale(cogresources,center=mean(cogresources,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_1_in_nfc <- interplot(m1_nfc,var1 = "InParty",var2='pidstrength')
m_1_in_nfc$data$CRT='Mean'
m_1_in_nfc$data$Cue='In'
m_1_out_nfc <- interplot(m1_nfc,var1 = "OutParty",var2='pidstrength')
m_1_out_nfc$data$CRT='Mean'
m_1_out_nfc$data$Cue='Out'

## Cog resources at +1
m2_nfc <- lm(DV1~InParty*scale(cogresources,center=mean(cogresources,na.rm=T)+sd(cogresources,na.rm=T),scale=F)*pidstrength+ OutParty*scale(cogresources,center=mean(cogresources,na.rm=T)+sd(cogresources,na.rm=T),scale=F)*pidstrength+age+female+ non_white +as.factor(education)+Republican_dummy,data)
m_2_in_nfc <- interplot(m2_nfc,var1 = "InParty",var2='pidstrength')
m_2_in_nfc$data$CRT='+1 SD'
m_2_in_nfc$data$Cue='In'
m_2_out_nfc <- interplot(m2_nfc,var1 = "OutParty",var2='pidstrength')
m_2_out_nfc$data$CRT='+1 SD'
m_2_out_nfc$data$Cue='Out'

forplot_cog <- rbind(m_0_in_nfc$data, m_0_out_nfc$data,m_1_in_nfc$data,m_1_out_nfc$data,m_2_in_nfc$data,m_2_out_nfc$data)
forplot_cog$battery      <- c("Cognitive\n resources")

#Combine plots
forplot_comb <- rbind(forplot, forplot_nfc, forplot_pk, forplot_cog)
forplot_comb$CRT <- factor(forplot_comb$CRT,levels = c("-1 SD","Mean","+1 SD"))
forplot_comb$battery <- factor(forplot_comb$battery,levels = c("CRT","NfC","Political\n knowledge", "Cognitive\n resources"))

farm_main<-ggplot(forplot_comb,aes(x=fake,y=coef1, colour=Cue))+geom_line(aes(linetype=Cue, color=Cue))+facet_grid(battery~CRT)+xlab("Partisan Social Identity Strength")+theme_bw()+ylab("Marginal effect of Party Cues on Policy Support ")+geom_ribbon(aes(ymin=lb,ymax=ub, fill=Cue),alpha=.4)+geom_hline(yintercept = 0,lty="dashed")+scale_fill_manual(values=c("dark green", "red"))+scale_colour_manual(values=c("black", "black")) + theme(strip.text.y = element_text(angle = 360), legend.position="bottom")
ggsave(farm_main, file="study2_farm.pdf",width=8,height=6)

#save data
save(data, file="Study2_data.RData")

